
package cn.com.pusai.core.util;

import java.io.*;
import java.util.*;
import java.util.zip.ZipException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

// Referenced classes of package cn.com.ecinc.core.util:
//			StringUtil, IOUtil

public class ZipUtil
{

	private static final int BUFFER = 0x1000000;
	private static final String SEPARATOR;
	private Set fileList;

	public ZipUtil()
	{
		fileList = new HashSet();
	}

	public Set getFileList()
	{
		return fileList;
	}

	public void setFileList(Set fileList)
	{
		this.fileList = fileList;
	}

	public String decompress(String zipFilePath)
		throws ZipException
	{
		if (StringUtil.isBlank(zipFilePath))
		{
			throw new IllegalArgumentException("������ڻ�Ϊ��");
		} else
		{
			File zipFile = new File(zipFilePath);
			return decompress(zipFile);
		}
	}

	public String decompress(String zipFilePath, String outputFolder)
		throws ZipException
	{
		if (StringUtil.isBlank(zipFilePath) || StringUtil.isBlank(outputFolder))
		{
			throw new IllegalArgumentException("������ڻ�Ϊ��");
		} else
		{
			File zipFile = new File(zipFilePath);
			File outputFile = new File(outputFolder);
			return decompress(zipFile, outputFile);
		}
	}

	public String decompress(File zipFile)
		throws ZipException
	{
		String outputFolder = IOUtil.getFileParent(zipFile);
		File outputFile = new File(outputFolder);
		return decompress(zipFile, outputFile);
	}
	
	/**
	 * 
	 * 
	 * */
	public String decompress(File zfile, File outputFile)throws ZipException{
		ZipFile zipFile;
		InputStream in;
		Exception exception;
		String filename = IOUtil.getFileNameNoExt(zfile);
		String extString = IOUtil.getFileExtension(zfile);
		if (StringUtil.isBlank(extString) || !extString.equalsIgnoreCase("zip"))
			throw new IllegalArgumentException("ѹ���ļ�������ȷ����.zip�ļ�");
		zipFile = null;
		in = null;
		FileOutputStream out = null;
		String s;
		try
		{
			zipFile = new ZipFile(zfile);
			Enumeration e = zipFile.getEntries();
			ZipEntry zipEntry = null;
			while (e.hasMoreElements()) 
			{
				zipEntry = (ZipEntry)e.nextElement();
				String name = zipEntry.getName();
				name = (new StringBuilder(String.valueOf(filename))).append("/").append(name).toString();
				if (zipEntry.isDirectory())
				{
					File dirFile = new File((new StringBuilder()).append(outputFile).append(SEPARATOR).append(name).toString());
					mkDirs(dirFile);
					fileList.add(dirFile);
				} else
				{
					if (name.indexOf("/") != -1)
					{
						String dir = name.substring(0, name.lastIndexOf("/"));
						File dirFile = new File((new StringBuilder()).append(outputFile).append(SEPARATOR).append(dir).toString());
						mkDirs(dirFile);
						fileList.add(dirFile);
					}
					File f = new File((new StringBuilder()).append(outputFile).append(SEPARATOR).append(name).toString());
					f.createNewFile();
					in = zipFile.getInputStream(zipEntry);
					out = new FileOutputStream(f);
					byte by[] = new byte[0x1000000];
					int c;
					while ((c = in.read(by)) != -1) 
						out.write(by, 0, c);
					fileList.add(f);
				}
			}
			s = (new StringBuilder()).append(outputFile).append(SEPARATOR).append(filename).toString();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			throw new ZipException("ZIP�ļ���ѹ�쳣");
		}
		finally
		{
			closeStream(new OutputStream[] {out});
		}
			//closeStream(new OutputStream[] {out});
		//	closeStream(new InputStream[] {in});
		try
		{
			if (zipFile != null)
				zipFile.close();
		}
		catch (Exception exception1) { }
		
			closeStream(new InputStream[]{in});
		try
		{
			if (zipFile != null)
				zipFile.close();
		}
		catch (Exception e) {
			
			e.printStackTrace();
		}
		return s;
		
	}

	private void closeStream(OutputStream os[])
	{
		OutputStream aoutputstream[];
		int j = (aoutputstream = os).length;
		for (int i = 0; i < j; i++)
		{
			OutputStream e = aoutputstream[i];
			if (os != null)
			{
				try
				{
					e.close();
				}
				catch (IOException ioe)
				{
					ioe.printStackTrace();
				}
				e = null;
			}
		}

	}

	private void closeStream(InputStream is[])
	{
		InputStream ainputstream[];
		int j = (ainputstream = is).length;
		for (int i = 0; i < j; i++)
		{
			InputStream e = ainputstream[i];
			if (is != null)
			{
				try
				{
					e.close();
				}
				catch (IOException ioe)
				{
					ioe.printStackTrace();
				}
				e = null;
			}
		}

	}

	private void mkDirs(File dir)
		throws Exception
	{
		try
		{
			if (!dir.exists())
				dir.mkdirs();
		}
		catch (Exception e)
		{
			System.out.println((new StringBuilder("����Ŀ¼�쳣��")).append(e.getMessage()).toString());
			e.printStackTrace();
			throw e;
		}
	}

	public static void main(String args[])
		throws ZipException
	{
		File file = new File("d:/1.zip");
		ZipUtil zipUtil = new ZipUtil();
		zipUtil.decompress(file);
		File f;
		for (Iterator iterator = zipUtil.getFileList().iterator(); iterator.hasNext(); System.out.println(f.toString()))
			f = (File)iterator.next();

	}

	static 
	{
		SEPARATOR = File.separator;
	}
}
